<?php
/**
 * Created by PhpStorm.
 * User: l-eru
 * Date: 2018/7/9
 * Time: 16:51
 */

namespace App\Library;


use Monolog\Formatter\HtmlFormatter;
use Monolog\Logger;

class Log
{
    const HEART_LISTEN   = '心跳监测';
    const MANAGER_FRAME  = '管理帧解析';
    const CONTROL_FRAME  = '控制帧解析';
    const DATA_FRAME     = '数据帧解析';
    const TOKEN_LISTEN   = 'token监测';
    const UNKNOWN_ERROR  = '未知错误';
    const CONNECT_CLOSED = '连接断开';

    protected static $instance = [];

    protected $prefix = '/home/dztech/log/';


    /**
     * @var Logger
     */
    protected $logger;

    protected function __construct($type, $tbMac)
    {
        $this->logger = new Logger($tbMac);

        $htmlHandler = new TanBaHandler($this->prefix . $type, $tbMac, 'html');
        $htmlHandler->setFormatter(new HtmlFormatter());
        $this->logger->pushHandler($htmlHandler);
    }

    /**
     * @param $type
     * @param $tbMac
     * @return Logger
     */
    protected static function getInstance($type, $tbMac)
    {
        $tbMac = $tbMac ?: 'no_mac';
        if (empty(self::$instance[$tbMac])) {
            self::$instance[$tbMac] = new self($type, $tbMac);
        }

        return self::$instance[$tbMac]->logger;
    }

    public static function cfgLog($tbMac)
    {
        return self::getInstance('config', $tbMac);
    }

    public static function dataLog($tbMac)
    {
        return self::getInstance('data', $tbMac);
    }

    public static function commonLog($tbMac)
    {
        return self::getInstance('common', $tbMac);
    }
}